package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.SpPredepositMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.exception.BusinessException;
import com.one.group.model.dto.PayerBillQueryDto;
import com.one.group.model.dto.PayerlistDto;
import com.one.group.model.dto.SpPredepositAddDto;
import com.one.group.model.entity.SpPredeposit;
import com.one.group.model.vo.SpPredepositVo;
import com.one.group.service.SpPredepositSercive;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SpPredepositSerciveImpl extends ServiceImpl<SpPredepositMapper, SpPredeposit> implements SpPredepositSercive {
    @Override
    public SpPredepositVo queryByPayer(PayerBillQueryDto payerBillQueryDto) {
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        SpPredepositVo spPredepositVo =getMapper().queryByPayer(payerBillQueryDto.getPayerId(),payerBillQueryDto.getPayerType(),tenant_id);
        if(spPredepositVo != null){
            return spPredepositVo;
        }else {
            spPredepositVo=getMapper().queryByPayer2(payerBillQueryDto.getPayerId(),payerBillQueryDto.getPayerType(),tenant_id);
            spPredepositVo.setAmount(BigDecimal.ZERO);
            return spPredepositVo;
        }
    }

    @Override
    public boolean predepositAdd(SpPredepositAddDto spPredepositAddDto) {
        try{
            //获取租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            SpPredepositVo spPredepositVo = getMapper().queryByPayer2(spPredepositAddDto.getPayerId(),spPredepositAddDto.getPayerType(),tenant_id);
            SpPredeposit spPredeposit =buildSpPredeposit(spPredepositVo);
            spPredeposit.setAmount(spPredepositAddDto.getAmount());
            return getMapper().insert(spPredeposit, true) != 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean predepositUpdate(SpPredepositAddDto spPredepositAddDto) {
        try{
            //获取租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            SpPredepositVo spPredepositVo = getMapper().queryByPayer(spPredepositAddDto.getPayerId(),spPredepositAddDto.getPayerType(),tenant_id);
            if(spPredepositVo == null){
                return predepositAdd(spPredepositAddDto);
            }
            SpPredeposit spPredeposit =buildSpPredeposit(spPredepositVo);
            spPredeposit.setId(spPredepositVo.getId());
            spPredeposit.setAmount(spPredepositVo.getAmount().add(spPredepositAddDto.getAmount()));
            return getMapper().update(spPredeposit, true) != 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SpPredepositVo> payerlist(PayerlistDto payerlistDto) {
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return getMapper().payerlist(payerlistDto.getName(), tenant_id);
    }

    @Override
    public BigDecimal totalamount() {
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return getMapper().totalamount(tenant_id);
    }

    private SpPredeposit buildSpPredeposit(SpPredepositVo spPredepositVo){
        SpPredeposit spPredeposit = new SpPredeposit();
        spPredeposit.setName(spPredepositVo.getName());
        spPredeposit.setPayerId(spPredepositVo.getPayerId());
        spPredeposit.setPayerType(spPredepositVo.getPayerType());
        spPredeposit.setPhone(spPredepositVo.getPhone());
        spPredeposit.setEmail(spPredepositVo.getEmail());
        spPredeposit.setAmount(spPredepositVo.getAmount());
        return spPredeposit;
    }
}
